package com.suning.sawp.member.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.suning.sawp.member.constant.MemberConstants;
import com.suning.sawp.member.dao.MemCustomerDao;
import com.suning.sawp.member.dto.CustomerDetail;
import com.suning.sawp.member.dto.MemCustomer;
import com.suning.sawp.member.dto.MemCustomerIntentCate;
import com.suning.sawp.member.dto.MemCustomerIntentCmmdty;
import com.suning.sawp.member.dto.MemFollowLog;
import com.suning.sawp.service.util.DateUtils;

/**
 * 拓展客户dao服务接口实现 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service(value = "memCustomerDao")
public class MemCustomerDaoImpl extends BaseDaoImpl implements MemCustomerDao {

	/**
	 * 保存客户关联信息 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param customer
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean saveMemCustomer(MemCustomer customer) {
		return super.save(customer);
	}

	public boolean saveMemCustomerDetail(CustomerDetail customerDetail) {
		return super.save(customerDetail);
	}

	/**
	 * 保存客户意向品类品牌 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cate
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean saveMemCustomerIntentCate(MemCustomerIntentCate cate) {
		return super.save(cate);
	}

	/**
	 * 保存客户意向商品 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cmmdty
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean saveMemCustomerIntentCmmdty(MemCustomerIntentCmmdty cmmdty) {
		return super.save(cmmdty);
	}

	/**
	 * 保存客户跟进记录 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param log
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean saveMemFollowLog(MemFollowLog log) {
		return super.save(log);
	}

	/**
	 * 更新客户关联信息 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param customer
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean updateMemCustomer(MemCustomer customer) {
		return super.update(customer);
	}

	public boolean updateMemCustomerDetail(CustomerDetail customerDetail) {
		return super.update(customerDetail);
	}

	/**
	 * 更新意向品类品牌 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cate
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean updateMemCustomerIntentCate(MemCustomerIntentCate cate) {
		return super.update(cate);
	}

	/**
	 * 更新意向商品 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cmmdty
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean updateMemCustomerIntentCmmdty(MemCustomerIntentCmmdty cmmdty) {
		return super.update(cmmdty);
	}

	/**
	 * 更新跟进记录 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param log
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean updateMemFollowLog(MemFollowLog log) {
		return super.update(log);
	}

	/**
	 * 批量保存意向品类品牌 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cates
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean batchSaveMemCustomerIntentCate(List<MemCustomerIntentCate> cates) {
		return super.batchSave("MEM_CUSTOMER.BATCH_SAVE_INTENT_CATE", cates);
	}

	/**
	 * 批量保存意向商品 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cmmdtys
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean batchSaveMemCustomerIntentCmmdty(List<MemCustomerIntentCmmdty> cmmdtys) {
		return super.batchSave("MEM_CUSTOMER.BATCH_SAVE_INTENT_CMMDTY", cmmdtys);
	}

	/**
	 * 批量更新客户详情
	 *
	 */
	@Override
	public boolean batchUpdateCustomerDetail(List<CustomerDetail> detailList) {
		return super.batchSave("MEM_CUSTOMER.BATCH_UPDATE_CUST_DETAIL", detailList);
	}

	public boolean batchUpdateCustomer(List<MemCustomer> customerList) {
		return super.batchSave("MEM_CUSTOMER.BATCH_UPDATE_CUSTOMER", customerList);
	}

	public boolean deleteIntentCateByDetailId(Long custDetailId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("detailId", custDetailId);
		return dalClient.execute("MEM_CUSTOMER.deleteIntentCateByDetailId", paramMap) > 0;
	}

	public boolean deleteIntentCmmdtyByDetailId(Long custDetailId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("detailId", custDetailId);
		return dalClient.execute("MEM_CUSTOMER.deleteIntentCmmdtyByDetailId", paramMap) > 0;
	}

	/**
	 * 查询客户信息列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param staffId
	 * @param custName
	 * @param buyDateType
	 * @param isToday
	 * @param currIndex
	 * @param size
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public List<MemCustomer> queryMyCustomers(String storeCode, String staffId, String custName, String buyDateType, boolean isToday, int currIndex, int size) {
		Date today = new Date();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("staffId", staffId);
		paramMap.put("storeCode", storeCode);

		// 客户名称或者手机号
		if (StringUtils.isNotBlank(custName)) {
			paramMap.put("custName", "%" + custName + "%");
		}

		// 预计购买时间区间类型 1-近一月，2-近2月
		if (MemberConstants.BUY_ON_ONE_MONTH.equals(buyDateType)) {
			paramMap.put("beginTime", DateUtils.getDateStrByFormat(today, "yyyy-MM-dd") + " 00:00:00");
			paramMap.put("endTime", DateUtils.getNextMonthStr(today, "yyyy-MM-dd", 1) + " 23:59:59");
		} else if (MemberConstants.BUY_ON_TWO_MONTH.equals(buyDateType)) {
			paramMap.put("beginTime", DateUtils.getDateStrByFormat(today, "yyyy-MM-dd") + " 00:00:00");
			paramMap.put("endTime", DateUtils.getNextMonthStr(today, "yyyy-MM-dd", 2) + " 23:59:59");
		}

		// 当日跟进
		if (isToday) {
			paramMap.put("today", DateUtils.getDateStrByFormat(today, "yyyyMMdd"));
		}

		paramMap.put("currIndex", currIndex);
		paramMap.put("size", size);
		return dalClientRead.queryForList("MEM_CUSTOMER.QUERY_MY_CUSTOMERS", paramMap, MemCustomer.class);
	}

	/**
	 * 根据客户ID查询客户信息 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param custId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public MemCustomer getMemCustomer(Long custId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("custId", custId);
		return dalClientRead.queryForObject("MEM_CUSTOMER.GET_MY_CUSTOMER", paramMap, MemCustomer.class);
	}

	/**
	 * 根据客户详情ID查询客户详情 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param detailId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public CustomerDetail getMemCustomerDetail(Long detailId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("detailId", detailId);
		return dalClientRead.queryForObject("MEM_CUSTOMER.GET_CUSTOMERS_DETAIL", paramMap, CustomerDetail.class);
	}

	/**
	 * 根据详情ID查询客户意向品类品牌列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param detailId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public List<MemCustomerIntentCate> queryMemCustomerIntentCates(Long detailId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("detailId", detailId);
		return dalClientRead.queryForList("MEM_CUSTOMER.QUERY_INTENT_CATES", paramMap, MemCustomerIntentCate.class);
	}

	/**
	 * 根据客户详情ID查询客户意向商品列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param detailId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public List<MemCustomerIntentCmmdty> queryMemCustomerIntentCmmdtys(Long detailId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("detailId", detailId);
		return dalClientRead.queryForList("MEM_CUSTOMER.QUERY_INTENT_CMMDTYS", paramMap, MemCustomerIntentCmmdty.class);
	}

	/**
	 * 查询客户跟进记录列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param staffId
	 * @param custId
	 * @param currIndex
	 * @param size
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public List<MemFollowLog> queryCustomerFollowList(String storeCode, String staffId, Long custId, int currIndex, int size) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("staffId", staffId);
		paramMap.put("storeCode", storeCode);
		paramMap.put("custId", custId);
		paramMap.put("currIndex", currIndex);
		paramMap.put("size", size);
		return dalClientRead.queryForList("MEM_CUSTOMER.QUERY_CUSTOMER_FOLLOWS", paramMap, MemFollowLog.class);
	}

	/**
	 * 查询共享客户列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param currIndex
	 * @param size
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public List<MemCustomer> queryShareCustomerList(String storeCode, String employeeName, int currIndex, int size) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("storeCode", storeCode);
		// 客户名称或者手机号
		if (StringUtils.isNotBlank(employeeName)) {
			paramMap.put("employeeName", "%" + employeeName + "%");
		}
		paramMap.put("currIndex", currIndex);
		paramMap.put("size", size);
		return dalClientRead.queryForList("MEM_CUSTOMER.QUERY_SHARE_CUSTOMERS", paramMap, MemCustomer.class);
	}

	/**
	 * 查询共享客户详情 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param custId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public MemCustomer getShareCustomer(String storeCode, Long custId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("storeCode", storeCode);
		paramMap.put("custId", custId);
		return dalClientRead.queryForObject("MEM_CUSTOMER.GET_SHARE_CUSTOMER_BY_ID", paramMap, MemCustomer.class);
	}

	/**
	 * 更新共享客户的关联员工信息 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param custId
	 * @param staffId
	 * @param staffName
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean updateShareCustomer(Long custId, String staffId, String staffName) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("staffId", staffId);
		paramMap.put("staffName", staffName);
		paramMap.put("custId", custId);
		int executeCnt = dalClient.execute("MEM_CUSTOMER.UPDATE_SHARE_CUSTOMER_BY_ID", paramMap);
		return executeCnt > 0;
	}

	/**
	 * 更新共享客户的客户详情信息 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param detailId
	 * @param staffId
	 * @param staffName
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public boolean updateCustomerDetail(Long detailId, String staffId, String staffName) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("staffId", staffId);
		paramMap.put("staffName", staffName);
		paramMap.put("detailId", detailId);
		int executeCnt = dalClient.execute("MEM_CUSTOMER.UPDATE_CUSTOMER_DETAIL_BY_ID", paramMap);
		return executeCnt > 0;
	}

	/**
	 * 查询潜客列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param staffId
	 * @param currIndex
	 * @param size
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public List<MemCustomer> queryPotentialCustomerList(String storeCode, String staffId, int currIndex, int size) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Date today = new Date();
		// 7天内预计购买的客户
		paramMap.put("beginTime", DateUtils.getDateStrByFormat(today, "yyyy-MM-dd") + " 00:00:00");
		paramMap.put("endTime", DateUtils.getDateStrByFormat(DateUtils.getNextDay(today, 6), "yyyy-MM-dd") + " 23:59:59");
		paramMap.put("storeCode", storeCode);
		paramMap.put("staffId", staffId);
		paramMap.put("currIndex", currIndex);
		paramMap.put("size", size);
		return dalClientRead.queryForList("MEM_CUSTOMER.QUERY_POTENTIAL_CUSTOMERS", paramMap, MemCustomer.class);
	}

	@Override
	public int dealPotentialCustomer() {

		Date today = new Date();
		Map<String, Object> paramMap = Maps.newHashMap();
		// 三天有效期
		paramMap.put("beginTime", DateUtils.getDateStrByFormat(today, "yyyy-MM-dd") + " 00:00:00");
		paramMap.put("endTime", DateUtils.getDateStrByFormat(DateUtils.getNextDay(today, 2), "yyyy-MM-dd") + " 23:59:59");

		// 七天之内预计购买
		paramMap.put("startDate", DateUtils.getDateStrByFormat(today, "yyyy-MM-dd") + " 00:00:00");
		paramMap.put("endDate", DateUtils.getDateStrByFormat(DateUtils.getNextDay(today, 6), "yyyy-MM-dd") + " 23:59:59");

		return dalClient.execute("MEM_CUSTOMER.SAVE_POTENTIAL_CUSTOMER_CARD", paramMap);
	}

	/**
	 * 修改员工客户为共享客户
	 */
	@Override
	public int updateToShareCustomer(String staffId) {
		Map<String, Object> paramMap = Maps.newHashMap();
		// 员工工号
		paramMap.put("staffId", staffId);
		// 更新时间
		paramMap.put("updateTime", new Date());
		return dalClient.execute("MEM_CUSTOMER.UPDATE_TO_SHARE_CUSTOMER", paramMap);
	}

	public int countMyCustomer(String staffId) {
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("staffId", staffId);
		return dalClientRead.queryForObject("MEM_CUSTOMER.countMyCustomer", paramMap, Integer.class);
	}

	public int countTodayWillFollowCustom(String staffId) {
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("staffId", staffId);
		return dalClientRead.queryForObject("MEM_CUSTOMER.countTodayWillFollowCustom", paramMap, Integer.class);
	}

	/**
	 * 查询我的客户
	 */
    @Override
    public MemCustomer getMyCustomer(String storeCode, String staffId, String custNo, String mobile) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("staffId", staffId);
        if (StringUtils.isNotBlank(custNo)) {
            paramMap.put("custNo", custNo);
        } else if (StringUtils.isNotBlank(mobile)) {
            paramMap.put("mobile", mobile);
        } else {
            return null;
        }
        return dalClientRead.queryForObject("MEM_CUSTOMER.GET_CUSTOMER_BY_PARAM", paramMap, MemCustomer.class);
    }

}
